package com.lq.memo.service.impl;

import com.google.gson.Gson;
import com.lq.email.util.MyUuid;
import com.lq.memo.entity.User;
import com.lq.memo.entity.WeChatModel;
import com.lq.memo.entity.WeChatSessionModel;
import com.lq.memo.exception.MyException;
import com.lq.memo.service.UserService;
import com.lq.memo.service.WchatLoginService;
import com.lq.memo.util.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @ClassName WchatLoginServiceImpl
 * @Description
 * @Author LQ
 * @Date 2021/11/19 0:01
 * @Version 1.0
 */
@Service
public class WchatLoginServiceImpl implements WchatLoginService {
    @Autowired
    UserService userService;

    @Autowired
    IdWorker idWorker;

    //在开发者平台中获取APPID和APPSECRET
    private static final String APPID = "wx13c62f4754e49c33";
    private static final String APPSECRET = "e9422236ca2644d4efd314053b8d784e";

    @Override
    public WeChatSessionModel wChatLogin(WeChatModel weChatModel) throws MyException {
        if (weChatModel == null || StringUtils.isBlank(weChatModel.getCode())) {
            throw new MyException("微信登录code不能为空");
        }
        //微信服务器的接口路径
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APPID + "&secret=" + APPSECRET + "&js_code=" + weChatModel.getCode() + "&grant_type=authorization_code";
        //进行网络请求,访问微信服务器接口
        ResponseEntity<String> responseEntity = new RestTemplate().exchange(url, HttpMethod.GET, null, String.class);
        WeChatSessionModel weChatSessionModel = null;

        //根据返回值进行后续操作
        if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
            String sessionData = responseEntity.getBody();
            Gson gson = new Gson();
            //将json字符串转化为实体类;
            weChatSessionModel = gson.fromJson(sessionData, WeChatSessionModel.class);
        }
        return weChatSessionModel;
    }

    @Override
    public User myLogin(WeChatSessionModel weChatSessionModel) throws MyException {
        if (weChatSessionModel == null) {
            throw new MyException("微信实体不能为空");
        }
        String openid = weChatSessionModel.getOpenid();
        String sessionKey = weChatSessionModel.getSession_key();
        String errcode = weChatSessionModel.getErrCode();
        String errmsg = weChatSessionModel.getErrMsg();
        if (openid == null || sessionKey == null) {
            throw new MyException("errcode: " + errcode + ";errmsg: " + errmsg);
        }
        User user = new User();
        user.setOpenId(openid);
        User queryUser = userService.getUser(user);
        if (queryUser != null && queryUser.getId() != null) {
            return queryUser;
        }
        // openid在数据库中没有,添加
        user.setCreateDate(new Date());
        user.setId(idWorker.nextId());
        int i = userService.saveUser(user);
        if (i > 0) {
            return user;
        } else {
            throw new MyException("添加用户失败");
        }
    }
}
